<template>
  <el-card shadow="never" class="aui-card--fill">
    <div class="mod-sys__user">
      <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
        <el-form-item label="文件名">
          <el-input v-model="dataForm.fileName" placeholder="文件名" clearable></el-input>
        </el-form-item>
        <el-form-item label="用户名">
          <el-input v-model="dataForm.userName" placeholder="用户名" clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="getDataList()">{{ $t('query') }}</el-button>
        </el-form-item>
      </el-form>
      <el-table
        v-loading="dataListLoading"
        :data="dataList"
        border
        @selection-change="dataListSelectionChangeHandle"
        style="width: 100%;">
<!--        <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>-->
        <el-table-column prop="fileName" label="文件名" header-align="center" align="center"></el-table-column>
        <el-table-column prop="path" label="路径" header-align="center" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.path">{{scope.row.path.replace(scope.row.fileName,'')}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="userName" label="用户名" header-align="center" align="center"></el-table-column>
        <el-table-column :label="$t('handle')" fixed="right" header-align="center" align="center" width="150">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="copyData(scope.row)">复制链接</el-button>
            <el-button type="text" size="small" @click="removeShare(scope.row.sign)">取消分享</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :current-page="page"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="limit"
        :total="total"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="pageSizeChangeHandle"
        @current-change="pageCurrentChangeHandle">
      </el-pagination>
    </div>
  </el-card>
</template>

<script>
import mixinViewModule from '@/mixins/view-module'
export default {
  mixins: [mixinViewModule],
  data () {
    return {
      mixinViewModuleOptions: {
        getDataListURL: '/oss/share/list',
        getDataListIsPage: false
      },
      dataForm: {
        userName: '',
        fileName: ''
      }
    }
  },
  methods: {
    // 复制链接
    copyData (row) {
      if (!row.sign) {
        return this.$message.error('复制失败')
      }
      let shareUrl = ''
      let curUrl = window.location.href
      let endIndex = curUrl.indexOf('#/')
      if (endIndex !== -1) {
        shareUrl = curUrl.substring(0, endIndex + 2) + 'share?sign=' + row.sign
      }
      this.copyHandle(`连接：${shareUrl}\n提取码：${row.password}`)
    },
    copyHandle (data) {
      let url = data
      let oInput = document.createElement('textarea')
      oInput.value = url
      document.body.appendChild(oInput)
      oInput.select() // 选择对象
      document.execCommand('Copy') // 执行浏览器复制命令
      document.body.removeChild(oInput)

      this.$message({
        message: '复制成功',
        type: 'success'
      })
      oInput.remove()
    },
    // 取消分享
    removeShare (sign) {
      if (!sign) {
        this.dataForm.passwordErrorMsg = '缺少参数'
        return
      }
      this.$confirm('是否确定取消分享?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const loading = this.$loading({
          lock: true,
          text: 'Loading',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.5)'
        })
        this.$http.post(`/oss/share/remove/${sign || ''}`).then(({ data: res }) => {
          loading.close()
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.getDataList()
        }).catch(() => {
          loading.close()
        })
      }).catch(() => {})
    }
  }
}
</script>
